package com.wang.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 调用链模型，存储从目标方法到Controller的完整调用路径
 */
public class CallChain {
    private final List<MethodInfo> chain;  // 调用路径列表

    public CallChain(List<MethodInfo> chain) {
        this.chain = new ArrayList<>(chain);  // 防御性拷贝，避免外部修改
    }

    // 转换为"方法1 -> 方法2 -> 方法3"格式的字符串
    public String getChainText() {
        return chain.stream()
                .map(MethodInfo::toString)
                .collect(Collectors.joining(" -> "));
    }

    // 获取调用链列表（返回副本避免外部修改）
    public List<MethodInfo> getChain() {
        return new ArrayList<>(chain);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CallChain callChain = (CallChain) o;
        return Objects.equals(chain, callChain.chain);
    }

    @Override
    public int hashCode() {
        return Objects.hash(chain);
    }

    @Override
    public String toString() {
        return getChainText();
    }
}
